package com.hnkywl.sts.service.base;

import com.hnkywl.sts.webservice.bg.util.EncryptUtil;
import com.hnkywl.sts.webservice.bg.util.StackTraceUtil;
import com.hnkywl.sts.webservice.wly.IF_ServiceStub;
import com.hnkywl.sts.webservice.wly.StsSysServiceStub;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.client.ServiceClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ResourceBundle;

/**
 * Created by Jerry on 13-12-31.
 */
@Service
public class WebserviceTransService {


    static ResourceBundle res = ResourceBundle.getBundle("webservice");
    private StsSysServiceStub stsSysServiceStub;
    private WebserviceLogService webserviceLogService;
    private IF_ServiceStub ifServiceStub;


    public String transhqmxWebServiceArgs(String json) {
        String result = null;
        try {
            String serialNumber = String.valueOf(System.currentTimeMillis());
            StsSysServiceStub.StsToWms sh = new StsSysServiceStub.StsToWms();
            sh.setSystemSecurityCode(res.getString("systemSecurityCode"));
            sh.setMemberCode(res.getString("memberCode"));
            sh.setArgs(json);
            sh.setSerialNumber(serialNumber);
            sh.setInterfaceId("STS_WMS_004");
            ServiceClient client = stsSysServiceStub._getServiceClient();
            String sightCon = res.getString("systemSecurityCode") + res.getString("memberCode") + json + serialNumber + "STS_WMS_004";
            generateESBSecurityHeader(sightCon, client);
            StsSysServiceStub.StsToWmsResponse postStoreNoticeFeedbackResponse = stsSysServiceStub.stsToWms(sh);
            result = postStoreNoticeFeedbackResponse.get_return();
            webserviceLogService.saveClientLog(
                    StsSysServiceStub.class.getName(),
                    "transZyfWebServiceArgs", json, result, "无异常", "");

        } catch (Exception e) {
            e.printStackTrace();
            webserviceLogService.saveClientLog(
                    StsSysServiceStub.class.getName(), "transZyfWebServiceArgs",
                    json, "【未返回数据】", StackTraceUtil.getStackTrace(e), "");
        }

        return result;
    }


    /**
     * 采购合同提交仓储入库
     *
     * @param json
     * @return
     */
    public String transCghtWebServiceArgs(String json) {
        String result = null;
        try {
            String serialNumber = String.valueOf(System.currentTimeMillis());
            StsSysServiceStub.StsToWms sh = new StsSysServiceStub.StsToWms();
            sh.setSystemSecurityCode(res.getString("systemSecurityCode"));
            sh.setMemberCode(res.getString("memberCode"));
            sh.setArgs(json);
            sh.setSerialNumber(serialNumber);
            sh.setInterfaceId("STS_WMS_001");
            ServiceClient client = stsSysServiceStub._getServiceClient();
            String sightCon = res.getString("systemSecurityCode") + res.getString("memberCode") + json + serialNumber + "STS_WMS_001";
            generateESBSecurityHeader(sightCon, client);
            StsSysServiceStub.StsToWmsResponse postStoreNoticeFeedbackResponse = stsSysServiceStub.stsToWms(sh);
            result = postStoreNoticeFeedbackResponse.get_return();
            webserviceLogService.saveClientLog(
                    StsSysServiceStub.class.getName(),
                    "transCghtWebServiceArgs", json, result, "无异常", "");

        } catch (Exception e) {
            e.printStackTrace();
            webserviceLogService.saveClientLog(
                    StsSysServiceStub.class.getName(), "transCghtWebServiceArgs",
                    json, "【未返回数据】", StackTraceUtil.getStackTrace(e), "");
        }

        return result;
    }


    /**
     * 放货通知
     *
     * @param json
     * @return
     */
    public String transXsdWebServiceArgs(String json) {
        String result = null;
        String exception = StringUtils.EMPTY;
        try {
            String serialNumber = String.valueOf(System.currentTimeMillis());
            StsSysServiceStub.StsToWms sh = new StsSysServiceStub.StsToWms();
            sh.setArgs(json);
            sh.setMemberCode(res.getString("memberCode"));
            sh.setSystemSecurityCode(res.getString("systemSecurityCode"));
            sh.setSerialNumber(serialNumber);
            sh.setInterfaceId("STS_WMS_002");
            ServiceClient client = stsSysServiceStub._getServiceClient();
            String sightCon = res.getString("systemSecurityCode") + res.getString("memberCode") + json + serialNumber + "STS_WMS_002";
            generateESBSecurityHeader(sightCon, client);
            StsSysServiceStub.StsToWmsResponse postStoreNoticeFeedbackResponse = stsSysServiceStub.stsToWms(sh);
            result = postStoreNoticeFeedbackResponse.get_return();
        } catch (Exception e) {
            e.printStackTrace();
            exception = StackTraceUtil.getStackTrace(e);
        }
        webserviceLogService.saveClientLog(
                StsSysServiceStub.class.getName(),
                "transXsdWebServiceArgs",
                json,
                result,
                exception,
                ""
        );
        return result;
    }


    /**
     * 推送赎货订单合同到销售系统
     *
     * @param
     * @param
     * @return
     * @throws Exception
     */
    public String putContractWebServiceArgs(String json) throws Exception {
        String result = null;
        try {
            IF_ServiceStub.PutOrder putOrder = new IF_ServiceStub.PutOrder();
            putOrder.setArgs(json);
            putOrder.setSystemSecurityCode(res.getString("systemSecurityCode"));
            ServiceClient client = stsSysServiceStub._getServiceClient();
            String sightCon = res.getString("systemSecurityCode") + res.getString("memberCode") + json;
            generateESBSecurityHeader(sightCon, client);
            IF_ServiceStub.PutOrderResponse putOrderResponse = ifServiceStub.putOrder(putOrder);
            result = putOrderResponse.get_return();
            webserviceLogService.saveClientLog(
                    IF_ServiceStub.class.getName(),
                    "putContractWebServiceArgs",
                    json,
                    result,
                    "",
                    ""
            );
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            String exception = StackTraceUtil.getStackTrace(e);
            webserviceLogService.saveClientLog(
                    IF_ServiceStub.class.getName(),
                    "putContractWebServiceArgs",
                    json,
                    result,
                    exception,
                    ""
            );
            throw e;
        }
    }


    private void generateESBSecurityHeader(String sightCon, ServiceClient client) {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace SecurityElementNamespace = factory.createOMNamespace(
                "", "");

        OMElement usernameEl = factory.createOMElement("username",
                SecurityElementNamespace);
        usernameEl.setText(res.getString("username"));
        OMElement passwordEl = factory.createOMElement("password",
                SecurityElementNamespace);
        passwordEl.setText(EncryptUtil.MD5(res.getString("password")));
        OMElement signTextEl = factory.createOMElement("signText",
                SecurityElementNamespace);
        String sighText = res.getString("sightCon") + sightCon;
        signTextEl.setText(EncryptUtil.MD5(sighText));
        System.out.println(signTextEl.getText());
        client.addHeader(usernameEl);
        client.addHeader(passwordEl);
        client.addHeader(signTextEl);
    }

    @Autowired
    public void setIfServiceStub(IF_ServiceStub ifServiceStub) {
        this.ifServiceStub = ifServiceStub;
    }

    @Autowired
    public void setStsSysServiceStub(StsSysServiceStub stsSysServiceStub) {
        this.stsSysServiceStub = stsSysServiceStub;
    }

    @Autowired
    public void setWebserviceLogService(WebserviceLogService webserviceLogService) {
        this.webserviceLogService = webserviceLogService;
    }

}
